package dao

import (
	"context"
	"fmt"
	"github.com/zeromicro/go-zero/core/stores/sqlc"
	"github.com/zeromicro/go-zero/core/stores/sqlx"
	"go-admin/base/utils"
	"strings"
)

var _ UserGroupModel = (*customUserGroupModel)(nil)

type (
	// UserGroupModel is an interface to be customized, add more methods here,
	// and implement the added methods in customUserGroupModel.
	UserGroupModel interface {
		FindByUserIds(ctx context.Context, userIds []int64) ([]*UserGroup, error)
		FindByGroupIds(ctx context.Context, groupIds []int64) ([]*UserGroup, error)
		FindByGroupIdAndUserId(ctx context.Context, groupId int64, userId int64) (*UserGroup, error)
		DeleteByIds(ctx context.Context, ids []int64) error
		BatchInsert(ctx context.Context, conn sqlx.SqlConn, data []*UserGroup) error
		userGroupModel
	}

	customUserGroupModel struct {
		*defaultUserGroupModel
	}
)

func (c customUserGroupModel) BatchInsert(ctx context.Context, conn sqlx.SqlConn, data []*UserGroup) error {
	query := fmt.Sprintf("insert into %s (%s) values (?, ?)", c.table, userGroupRowsExpectAutoSet)
	bulkInserter, _ := sqlx.NewBulkInserter(conn, query)
	defer bulkInserter.Flush()
	for _, userGroup := range data {
		err := bulkInserter.Insert(userGroup.UserId, userGroup.GroupId)
		if err != nil {
			return err
		}
	}
	return nil
}

func (c customUserGroupModel) FindByGroupIdAndUserId(ctx context.Context, groupId int64, userId int64) (*UserGroup, error) {
	query := fmt.Sprintf("select %s from %s where `user_id` = ? and `group_id` = ? ", userGroupRows, c.table)
	var resp *UserGroup
	err := c.conn.QueryRowCtx(ctx, &resp, query, userId, groupId)
	switch err {
	case nil:
		return resp, nil
	case sqlc.ErrNotFound:
		return nil, ErrNotFound
	default:
		return nil, err
	}
}

func (c customUserGroupModel) FindByUserIds(ctx context.Context, userIds []int64) ([]*UserGroup, error) {
	query := fmt.Sprintf("select %s from %s where `user_id` in ( %s ) ", userGroupRows, c.table, strings.Join(utils.GeneratePlaceholderByInt64(userIds), ","))
	var resp []*UserGroup
	err := c.conn.QueryRowsCtx(ctx, &resp, query, utils.ConvertToAny(userIds)...)
	switch err {
	case nil:
		return resp, nil
	case sqlc.ErrNotFound:
		return nil, ErrNotFound
	default:
		return nil, err
	}
}

func (c customUserGroupModel) FindByGroupIds(ctx context.Context, groupIds []int64) ([]*UserGroup, error) {
	query := fmt.Sprintf("select %s from %s where `group_id` in ( %s ) ", userGroupRows, c.table, strings.Join(utils.GeneratePlaceholderByInt64(groupIds), ","))
	var resp []*UserGroup
	err := c.conn.QueryRowsCtx(ctx, &resp, query, utils.ConvertToAny(groupIds)...)
	switch err {
	case nil:
		return resp, nil
	case sqlc.ErrNotFound:
		return nil, ErrNotFound
	default:
		return nil, err
	}
}

func (c customUserGroupModel) DeleteByIds(ctx context.Context, ids []int64) error {
	query := fmt.Sprintf("delete from %s where `id` in ( %s )", c.table, strings.Join(utils.GeneratePlaceholderByInt64(ids), ","))
	_, err := c.conn.ExecCtx(ctx, query, utils.ConvertToAny(ids)...)
	return err
}

// NewUserGroupModel returns a model for the database table.
func NewUserGroupModel(conn sqlx.SqlConn) UserGroupModel {
	return &customUserGroupModel{
		defaultUserGroupModel: newUserGroupModel(conn),
	}
}
